package com.dyh.quickdemozwei.conditionOperation;


import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.function.BiPredicate;

public interface ConditionOperations {

 enum Types{
     EQUALS,
     NOT_EQUALS,
     IN,
     NOT_IN
 }

    /*1==1 ==1L == "1" */
  BiPredicate<Object,Object> EQUALS = (a,b)->{
      if (a==null)
          return b == null;
      if (b==null)
          return false;
      return a.toString().equals(b.toString());
    };


    BiPredicate<Object,Object> NOT_EQUALS = (a,b)->{
        if (a==null)
            return b != null;
        if (b==null)
            return true;
        return !a.toString().equals(b.toString());
    };




    BiPredicate<Object,Object> IN = (a,b)->{
        if (b instanceof List){
            if (a==null)
                return false;
            List that = (List)b;
            if (CollectionUtils.isEmpty(that))
                return false;
            return that.contains(a);
        }
        return false;

    };


    BiPredicate<Object, Object> NOT_IN = (a,b)->{
        if (b instanceof List){
            if (a==null)
                return false;
            List that = (List)b;

            if (CollectionUtils.isEmpty(that))
                return true;
            return !that.contains(a);
        }
        return false;
    };

    static  BiPredicate getByType(Types types){
        switch (types){
            case EQUALS:
                return EQUALS;
            case NOT_EQUALS:
                return NOT_EQUALS;
            case IN:
                return IN;
            case NOT_IN:
                return NOT_IN;
        }
        throw new RuntimeException("找不运算类型");
    }


    public static void main(String[] args) {

        System.out.println(getByType(Types.EQUALS).test("a","a")
        );

//        System.out.println(NOT_EQUALS.test(null,null));
//        System.out.println(NOT_EQUALS.test(null,"a"));
//        System.out.println(NOT_EQUALS.test("a",null));
//
//
//
//        System.out.println(NOT_EQUALS.test(1,1));
//        System.out.println(NOT_EQUALS.test(1,1L));
//        System.out.println(NOT_EQUALS.test(1,"1"));
//        System.out.println(NOT_EQUALS.test(1,1.0f));


//        System.out.println(IN.test("a", Lists.newArrayList("a","b")));
//        System.out.println(IN.test("a", Lists.newArrayList("b")));
//
//        System.out.println(NOT_IN.test("a", Lists.newArrayList("a","b")));
//        System.out.println(NOT_IN.test("a", Lists.newArrayList("b")));
        ;

//        System.out.println(EQUALS.test(null,null));
//        System.out.println(EQUALS.test(null,"a"));
//        System.out.println(EQUALS.test("a",null));
//
//        System.out.println(EQUALS.test(1,1));
//        System.out.println(EQUALS.test(1,1L));
//        System.out.println(EQUALS.test(1,"1"));
//        System.out.println(EQUALS.test(1,1.0f));//false

    }
}
